Here are a few examples illustrating different scenarios with 3D arrays in Java:
Initializing a 3D Array with Random Values
Initializing a 3D Array with Random Values in Java
import java.util.Random;
public class Main{
public static void main(String[] args) {
int[][][] threeDArray = new int[3][4][5];
Random random = new Random();
// Initializing 3D array with random values
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 5; k++) {
threeDArray[i][j][k] = random.nextInt(100); // Generating random values
}
}
}
// Displaying the 3D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 5; k++) {
System.out.print(threeDArray[i][j][k] + " ");
}
System.out.println();
}
System.out.println();
}
}
}
Output
32 14 55 48 85
91 58 65 72 79
93 57 4 56 33
8 36 31 32 73
79 78 72 14 87
81 39 14 37 31
82 79 69 15 35
3 83 55 46 14
73 36 0 19 13
4 7 12 51 97
66 56 33 7 80
9 11 82 81 19
This code initializes a 3D array threeDArray with dimensions [3][4][5] and fills it with random integer values between 0 and 99 using Random.nextInt(100).
Finding the Sum of Elements in a 3D Array
Calculating the Sum of values in a 3D Array
public class Main{
public static void main(String[] args) {
int[][][] threeDArray = {
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
},
{
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
}
};
int sum = 0;
// Calculating the sum of all elements in the 3D array
for (int[][] matrix : threeDArray) {
for (int[] row : matrix) {
for (int element : row) {
sum += element;
}
}
}
System.out.println("Sum of all elements in the 3D array: " + sum);
}
}
Output
Sum of all elements in the 3D array: 171
This code calculates the sum of all elements in a pre-defined 3D array threeDArray using nested loops to traverse through each element.
3D Array for Representing a 3D Image
3D Array for Representing a 3D Image
public class Main{
public static void main(String[] args) {
// Create a 3D array to store an RGB image
int[][][] imageData = new int[3][2][2];
// Set pixel values
imageData[0][0][0] = 255; // Red pixel
imageData[1][0][0] = 0; // Green pixel
imageData[2][0][0] = 0; // Blue pixel
imageData[0][1][0] = 0; // Red pixel
imageData[1][1][0] = 255; // Green pixel
imageData[2][1][0] = 0; // Blue pixel
imageData[0][0][1] = 0; // Red pixel
imageData[1][0][1] = 0; // Green pixel
imageData[2][0][1] = 255; // Blue pixel
imageData[0][1][1] = 255; // Red pixel
imageData[1][1][1] = 255; // Green pixel
imageData[2][1][1] = 255; // Blue pixel
// Print the image data
for (int layer = 0; layer < 3; layer++) {
System.out.println("Layer " + layer + ":");
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) {
System.out.print(imageData[layer][row][col] + " ");
}
System.out.println();
}
}
}
}
Output
Layer 0:
255 0
0 255
Layer 1:
0 0
255 255
Layer 2:
0 255
0 255
This code represents a simple RGB image using a 3D array imageData with dimensions [3][2][2]. It sets RGB pixel values for a 2x2 image.
Displaying 3D Array values
Displaying 3D Array values in Java
public class Main{
public static void main(String[] args) {
// Declaration and initialization of a 3D array (2x3x4)
int[][][] threeDArray = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};
// Accessing elements in the 3D array
System.out.println("Element at [0][1][2]: " + threeDArray[0][1][2]); // Output: 7
System.out.println("Element at [1][2][3]: " + threeDArray[1][2][3]); // Output: 24
}
}
Output
Element at [0][1][2]: 7
Element at [1][2][3]: 24
This code declares and initializes a 3D array threeDArray and then accesses specific elements by their indices to demonstrate how to access values in a 3D array.